package com.nervenets.web.controller.handler.user.help;

import com.alibaba.fastjson.JSONArray;
import com.nervenets.utils.StringUtils;
import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.common.TokenParams;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.hibernate.domain.user.Feedback;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class FeedbackSubmitHandler extends Handler<FeedbackSubmitParams> {
    @Override
    public String handle(FeedbackSubmitParams params, HttpServletRequest request) throws Exception {
        long userId = 0;
        if (!StringUtils.isEmpty(params.getToken())) {
            userId = TokenParams.resolveToken(params.getToken());
        }

        Feedback feedback = new Feedback();
        feedback.setUserId(userId);
        feedback.setType(params.getType());
        feedback.setContent(params.getContent());
        try {
            final List<String> pictures = JSONArray.parseArray(params.getPictures(), String.class);
            feedback.setPictures(pictures);
        } catch (Exception e) {
            return errorMessage(MessageCode.code_444, "图片格式有误！");
        }
        feedback.setName(params.getName());
        feedback.setMobile(params.getMobile());
        userDao.saveOrUpdate(feedback);
        return successMessage();
    }

    @Override
    public String result() throws Exception {
        return null;
    }
}
